package com.kl.modular.sysman.system.controller;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import com.baomidou.mybatisplus.plugins.Page;
import com.kl.modular.common.controller.BaseController;
import com.kl.modular.common.response.ResponseBase;
import com.kl.modular.system.dao.model.SystemDicType;
import com.kl.modular.system.service.api.ISystemDicTypeService;
import com.kl.modular.system.service.bo.SystemDicTypeBo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.validation.BindingResult;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 数据字典类型控制器
 * @author xr_zjb
 * @date 2017-08-07 15:58
 *
 */
@Controller
public class SystemDicTypeController extends BaseController {
	
	private static final Logger logger = LoggerFactory.getLogger(SystemDicTypeController.class);
	
	@Autowired
	private ISystemDicTypeService systemDicTypeService;
	
	/** 数据字典类型 view */
	private static String pathSystemDicTypeRoot="/sysman/system/dicType/";
	
	
	/**
	 * 数据字典类型列表页面
	 */
	@RequestMapping(value = "/sysman/systemDicType/systemDicTypeList", method = RequestMethod.GET)
	@RequiresPermissions("sysman:systemDicType_man:view")
	public String queryList(HttpServletRequest request) {
		logger.info("数据字典类型列表页面");
		return pathSystemDicTypeRoot + "systemDicTypeList.ftl";
	}

	/**
	 * 数据字典类型列表数据
	 */
	@RequestMapping(value = "/sysman/systemDicType/systemDicTypeAjaxList", method = RequestMethod.POST)
	@RequiresPermissions("sysman:systemDicType_man:view")
	public String queryAjaxList(SystemDicTypeBo systemDicTypeBo, Page<SystemDicType> page, HttpServletRequest request) {
		logger.info("systemDicType列表数据>>data:" + JSON.toJSONString(systemDicTypeBo));
		page = systemDicTypeService.selectPageBySystemDicTypeBo(page, systemDicTypeBo);
		request.setAttribute("pager", page);
		return pathSystemDicTypeRoot + "systemDicTypeAjaxList.ftl";
	}
	
	
	/**
	 * 数据字典类型添加页面
	 * @return
	 */
	@RequestMapping(value="/sysman/systemDicType/toSystemDicTypeAdd" , method = RequestMethod.GET)
	@RequiresPermissions("sysman:systemDicType_man:add")
	public String toAddSystemDicType(){
		logger.info("数据字典类型添加页面");
		return pathSystemDicTypeRoot + "addSystemDicType.ftl";
	}
	
	/**
	 * 添加数据字典类型
	 */
	@RequestMapping(value="/sysman/systemDicType/addSystemDicType" , method = RequestMethod.POST)
	@ResponseBody
	@RequiresPermissions("sysman:systemDicType_man:add")
	public String addSystemDicType(@Valid SystemDicType systemDicType, BindingResult result, HttpServletResponse response) throws Exception{
		logger.info("添加数据字典类型>>data:" + JSON.toJSONString(systemDicType));

		if (result.hasErrors()) {
			return ResponseBase.errorStr(result.getAllErrors().get(0).getDefaultMessage());
		}

		systemDicType.setCreateTime(new Date());
		systemDicType.setModifyTime(new Date());
		systemDicTypeService.insert(systemDicType);

		String jsonstr = ResponseBase.success();
		logger.info("添加数据字典类型>>返回>>data:" + jsonstr);
		return jsonstr;
	}

	
	/**
	 * 数据字典类型更新页面
	 * @return
	 */
	@RequestMapping(value = "/sysman/systemDicType/toChangeView" , method = RequestMethod.GET)
	@RequiresPermissions("sysman:systemDicType_man:update")
	public String toChangeSystemDicType(Long id, HttpServletRequest request) throws Exception{
		logger.info("数据字典类型修改页面>>data:" + id);
		SystemDicType systemDicType = systemDicTypeService.selectById(id);
		request.setAttribute("systemDicType", systemDicType);
		return pathSystemDicTypeRoot + "systemDicTypeUpChange.ftl";
	}

	
	/**	
	 * 修改
	 */
	@RequestMapping(value = "/sysman/systemDicType/upChange" , method = RequestMethod.POST)
	@ResponseBody
	@RequiresPermissions("sysman:systemDicType_man:update")
	public String upChangeSystemDicType(@Valid SystemDicType systemDicType, BindingResult result, HttpServletResponse response) throws Exception{
		logger.info("更新数据字典类型信息>>data:" + JSON.toJSONString(systemDicType));

		if (result.hasErrors()) {
			return ResponseBase.errorStr(result.getAllErrors().get(0).getDefaultMessage());
		}
		if(systemDicType.getId() == null){
			return ResponseBase.errorStr(ResponseBase.RESPOSNE_NOT_VALID_ERROR);
		}

		systemDicType.setModifyTime(new Date());
		systemDicTypeService.updateById(systemDicType);

		String jsonstr = ResponseBase.success();
		logger.info("编辑数据字典类型信息>>返回>>data:" + jsonstr);
		return jsonstr;
	}

	/**
	 * 批量删除数据字典类型
	 */
	@RequestMapping(value = "/sysman/systemDicType/delSystemDicType" , method = RequestMethod.POST)
	@ResponseBody
	@RequiresPermissions("sysman:systemDicType_man:delete")
	public String delSystemDicType(String id,HttpServletResponse response) throws Exception{
		logger.info("批量删除数据字典类型>>data:" + JSON.toJSONString(id));

		// 数据验证
		if (StringUtils.isEmpty(id)) {
			return ResponseBase.errorStr(ResponseBase.RESPOSNE_NOT_VALID_ERROR);
		}

		String[] codestrs = id.split(",");

		systemDicTypeService.logicalDeleteBatch(codestrs);

		String jsonstr = ResponseBase.success();
		logger.info("删除数据字典类型资源>>返回view>>data:" + jsonstr);
		return jsonstr;
	}
}
